<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试工具</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3>API测试工具</h3>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">API Base URL</label>
                            <input type="text" class="form-control" id="apiUrl" value="http://localhost:9090/api">
                        </div>
                        
                        <div class="mb-3">
                            <h5>注册测试</h5>
                            <div class="row">
                                <div class="col-md-6">
                                    <input type="text" class="form-control mb-2" id="regUsername" placeholder="用户名" value="testuser2">
                                    <input type="password" class="form-control mb-2" id="regPassword" placeholder="密码" value="Test123">
                                    <input type="password" class="form-control mb-2" id="regConfirmPassword" placeholder="确认密码" value="Test123">
                                </div>
                                <div class="col-md-6">
                                    <input type="text" class="form-control mb-2" id="regFullName" placeholder="真实姓名" value="测试用户2">
                                    <input type="email" class="form-control mb-2" id="regEmail" placeholder="邮箱" value="test2@example.com">
                                    <select class="form-control mb-2" id="regRole">
                                        <option value="customer">客户</option>
                                        <option value="agent">客服</option>
                                        <option value="admin">管理员</option>
                                    </select>
                                </div>
                            </div>
                            <button class="btn btn-primary" onclick="testRegister()">测试注册</button>
                        </div>
                        
                        <div class="mb-3">
                            <h5>登录测试</h5>
                            <div class="row">
                                <div class="col-md-6">
                                    <input type="text" class="form-control mb-2" id="loginUsername" placeholder="用户名" value="testuser2">
                                    <input type="password" class="form-control mb-2" id="loginPassword" placeholder="密码" value="Test123">
                                </div>
                            </div>
                            <button class="btn btn-success" onclick="testLogin()">测试登录</button>
                        </div>
                        
                        <div class="mt-4">
                            <h5>响应结果</h5>
                            <pre id="responseArea" class="bg-light p-3" style="min-height: 200px;">点击按钮开始测试...</pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:9090/api';

        async function testRegister() {
            const responseArea = document.getElementById('responseArea');
            responseArea.textContent = '正在测试注册...';
            
            const data = {
                username: document.getElementById('regUsername').value,
                password: document.getElementById('regPassword').value,
                confirmPassword: document.getElementById('regConfirmPassword').value,
                fullName: document.getElementById('regFullName').value,
                email: document.getElementById('regEmail').value,
                role: document.getElementById('regRole').value
            };

            try {
                console.log('发送注册请求:', data);
                const response = await fetch(`${API_BASE_URL}/auth/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                });

                console.log('响应状态:', response.status);
                const result = await response.json();
                console.log('响应数据:', result);
                
                responseArea.textContent = JSON.stringify(result, null, 2);
                
                if (result.success) {
                    responseArea.style.color = 'green';
                } else {
                    responseArea.style.color = 'red';
                }
            } catch (error) {
                console.error('注册错误:', error);
                responseArea.textContent = `错误: ${error.message}`;
                responseArea.style.color = 'red';
            }
        }

        async function testLogin() {
            const responseArea = document.getElementById('responseArea');
            responseArea.textContent = '正在测试登录...';
            
            const data = {
                username: document.getElementById('loginUsername').value,
                password: document.getElementById('loginPassword').value
            };

            try {
                console.log('发送登录请求:', data);
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                });

                console.log('响应状态:', response.status);
                const result = await response.json();
                console.log('响应数据:', result);
                
                responseArea.textContent = JSON.stringify(result, null, 2);
                
                if (result.success) {
                    responseArea.style.color = 'green';
                } else {
                    responseArea.style.color = 'red';
                }
            } catch (error) {
                console.error('登录错误:', error);
                responseArea.textContent = `错误: ${error.message}`;
                responseArea.style.color = 'red';
            }
        }
    </script>
</body>
</html>
